/*
 * Copyright (C) 2010 Rodion Volkov (volkov.rodion@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rchik.divermate;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.entity.mime.MIME;
import org.apache.http.entity.mime.content.AbstractContentBody;

 public class LimitedInputStreamBody extends AbstractContentBody {
 
     private final InputStream stream;
     private final String filename;
     private final String charset;
     private final long total;

    
     public LimitedInputStreamBody(final InputStream stream,
                     final String filename,
                     final String mimeType,
                     final String charset,
                     final long total) {
         super(mimeType);
         if (stream == null) {
             throw new IllegalArgumentException("Stream may not be null");
         }
         this.stream = stream;
         if (filename != null)
             this.filename = filename;
         else
             this.filename = "stream.body";
         this.charset = charset;
         this.total = total;
     }
   
     public LimitedInputStreamBody(final InputStream stream,
                     final String mimeType,
                     final String charset,
                     final long total) {
         this(stream, null, mimeType, charset, total);
     }
 
     public LimitedInputStreamBody(final InputStream stream, final String mimeType, final long total) {
         this(stream, mimeType, null, total);
     }
 
     public LimitedInputStreamBody(final InputStream stream, final long total) {
         this(stream, "application/octet-stream", total);
     }
 
     public InputStream getInputStream() throws IOException {
         return stream;
     }
   
     @Deprecated
     public void writeTo(final OutputStream out, int mode) throws IOException {
         writeTo(out);
     }
 
     public void writeTo(final OutputStream out) throws IOException {
         if (out == null) {
             throw new IllegalArgumentException("Output stream may not be null");
         }
        InputStream in = stream;
        try {
            byte[] tmp = new byte[4096];
            int l;
            while ((l = in.read(tmp)) != -1) {
                out.write(tmp, 0, l);
            }
            out.flush();
        } finally {
            in.close();
        }
    }

    public String getTransferEncoding() {
        return MIME.ENC_BINARY;
    }

    public String getCharset() {
        return charset;
    }

    public long getContentLength() {
        return total;
    }

    public String getFilename() {
        return '@'+filename;
    }
}